package simpleftp.client.control;

import java.io.File;
import java.util.concurrent.Callable;

import simpleftp.client.gui.FTPClientWindow;

/**
 * Simple class to upload a file to the server
 * Create a connection to the server on the given directory and upload given file
 */
public final class Uploader extends Loader implements Callable<Boolean> {
	
	private File file = null;

	public Uploader(File f, String currentServerDir, FTPClientWindow window, String server, String port,
			String user, String pass) {
		super(currentServerDir, window, server, port, user, pass);
		
		this.file = f;
	}

	@Override
	public Boolean call() throws Exception {
		System.out.println("UPLOAD starting for " + file.getName());
		Boolean result = false;
		client.connect(server, Integer.parseInt(port), user, pass);
		result = client.stor(file);
		if (result) {
			wind.console.append("\nSuccessfully transfered file "
							+ file.getName());
			System.out.println("Successfully transfered file "
							+ file.getName());
			listDir();
		} else {
			wind.console.append("\nFailed transfering file " + file.getName());
			System.out.println("Failed transfering file "
							+ file.getName());
		}
		client.disconnect();
		return result;
	}
}
